package com.example.springboote.service;


import com.example.springboote.domian.User;
import com.example.springboote.repositroy.UserRepositroy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    @Resource
    private UserRepositroy userRepositroy;

    public List<User>  findByAll(){
        return userRepositroy.findAll();
    }

    public User findByUsername(String username){
        User user = null;
        List<User> byUsername = userRepositroy.findByUsername(username);
        if (byUsername != null && !byUsername.isEmpty()){
            user = byUsername.get(0);
        }
        return user;
    }


    public String delete(String id){
        try {
            User user = userRepositroy.findById(id).get();
            if (user != null){
                userRepositroy.delete(user);
                return "1";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "2";
        }
        return "2";
    }


    public User add(User user){
        User save = userRepositroy.save(user);
        return save;
    }
}
